//
//  NetworkStatusManager.m
//  Peso Guide
//
//  Created by macbook on 2025/6/6.
//

#import "NetworkStatusManager.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <ifaddrs.h>
#import <net/if.h>
#import <netinet/in.h>
#import <arpa/inet.h>

@implementation NetworkStatusManager

// 检测VPN连接状态
+ (BOOL)isVPNConnected {
    BOOL flag = NO;
    NSString *version = [UIDevice currentDevice].systemVersion;
    // 需要适配不同iOS版本
    if (version.doubleValue >= 9.0) {
        NSDictionary *dict = CFBridgingRelease(CFNetworkCopySystemProxySettings());
        NSArray *keys = [dict[@"__SCOPED__"] allKeys];
        for (NSString *key in keys) {
            if ([key rangeOfString:@"tap"].location != NSNotFound ||
                [key rangeOfString:@"tun"].location != NSNotFound ||
                [key rangeOfString:@"ipsec"].location != NSNotFound ||
                [key rangeOfString:@"ppp"].location != NSNotFound) {
                flag = YES;
                break;
            }
        }
    }
    
    // 备用方法：检查网络接口
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    success = getifaddrs(&interfaces);
    if (success == 0) {
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];
            if ([name rangeOfString:@"tap"].location != NSNotFound ||
                [name rangeOfString:@"tun"].location != NSNotFound ||
                [name rangeOfString:@"ipsec"].location != NSNotFound ||
                [name rangeOfString:@"ppp"].location != NSNotFound) {
                flag = YES;
                break;
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    freeifaddrs(interfaces);
    return flag;
}

// 检测代理状态
+ (NetworkProxyStatus)proxyStatus {
    CFDictionaryRef proxySettings = CFNetworkCopySystemProxySettings();
    CFArrayRef proxies = CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"https://www.apple.com"]), proxySettings);
    
    NSArray *proxiesArray = (__bridge NSArray *)proxies;
    
    if (proxiesArray.count > 0) {
        NSDictionary *settings = proxiesArray[0];
        // 检测代理类型
        NSString *proxyType = settings[(NSString *)kCFProxyTypeKey];
        
        if ([proxyType isEqualToString:(NSString *)kCFProxyTypeNone]) {
            CFRelease(proxySettings);
            CFRelease(proxies);
            return NetworkProxyStatusDisabled;
        } else {
            CFRelease(proxySettings);
            CFRelease(proxies);
            return NetworkProxyStatusEnabled;
        }
    }
    
    CFRelease(proxySettings);
    CFRelease(proxies);
    return NetworkProxyStatusUnknown;
}

// 获取代理状态
+ (BOOL)proxyStatusString {
    NetworkProxyStatus status = [self proxyStatus];
    switch (status) {
        case NetworkProxyStatusEnabled: return YES;//已开启代理
        case NetworkProxyStatusDisabled: return NO;//未开启代理
        default: return NO;//未知状态
    }
}

@end
